//
//  ChangeDeviceNameVC.m
//  KEREN
//
//  Created by MW on 2023/3/14.
//

#import "ChangeDeviceNameVC.h"

@interface ChangeDeviceNameVC ()
@property (nonatomic, strong) UITextField *textField;
@end

@implementation ChangeDeviceNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //TODO 国际化
    [self setNavViewWithRightTitle:@"保存" navColorIswhite:YES withTitle:@"车辆名称设置"];
    WEAKSELF
    self.rightClickBlock = ^(void){
        [weakSelf updetaDeviceName];
    };
    [self setViewLayout];
}

- (void)updetaDeviceName{
    WEAKSELF
    NSDictionary *parameters = @{@"ebike_id":@(self.deviceInfo.Id),@"ebike_name":_textField.text};
    [NetworkingTool POST:Device_message_update parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        if([response[@"code"] intValue] == 0){
            weakSelf.deviceInfo.name = weakSelf.textField.text;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            //[[KRAlartManager shareInstance] showErrorAlert:response[@"msg"]];
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        
    }];
}

- (void)setViewLayout{
    _textField = [UITextField setFlieldplaceholder:@"" Font:Font(16) borderColor:[UIColor clearColor]];

    _textField.frame = CGRectMake(0, NAVTOP_Height, DEVICE_WIDTH, 60);
    [self.view addSubview:_textField];
    _textField.backgroundColor = Whire_Color;
    _textField.textColor = Color_333333;
    _textField.layer.cornerRadius = 0;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:_textField];

    UIButton *clearBtn = [UIButton setButtonWithImage:@"clear" cornerRadius:0 backColor:[UIColor clearColor]];
    [self.view addSubview:clearBtn];
    _textField.text = self.deviceInfo.name;
    [clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.width.height.offset(21);
        make.centerY.equalTo(_textField);
    }];
    [UIView setLineViewWithframe:CGRectMake(24, 59, DEVICE_WIDTH - 48, 1) forinView:_textField];
    
    UILabel *tipsLbl = [[UILabel alloc] init];
    [self.view addSubview:tipsLbl];
    [tipsLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.top.equalTo(_textField.mas_bottom).offset(12);
    }];
    tipsLbl.textColor = Color_86898E;
    tipsLbl.font = Font(12);
    //TODO 国际化
    tipsLbl.text = @"设备名称不可与其他设备重复";
    [clearBtn addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clear{
    _textField.text = @"";
}

-(void)textFiledEditChanged:(NSNotification*)notification{
    UITextField *textField = notification.object;
    NSString *str = textField.text;
    if(str.length > 16 && textField.markedTextRange == nil){
        str =  [str substringToIndex:16];
        textField.text = str;
    }
}

@end
